package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Turn;

public class TurnDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Turn turn) {
		this.sql = "INSERT INTO Turns (description_turn) VALUES (?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, turn.getDescriptionTurn());
			this.statement.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public void update(Turn turn) {
		open();
		this.sql = "UPDATE Turns SET description_turn = ? WHERE id_turn = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, turn.getDescriptionTurn());
			this.statement.setInt(2, turn.getIdTurn());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Turn turn) {
		open();
		this.sql = "DELETE FROM Turns WHERE id_turn = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, turn.getIdTurn());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Turn search(Integer id_turn) {
		open();
		this.sql = "SELECT * FROM Turns WHERE id_turn = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_turn);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Turn(result.getInt("id_turn"),
						result.getString("description_turn"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Turn> list() {
		open();
		this.sql = "SELECT * FROM Turns";
		ArrayList<Turn> turns = new ArrayList<Turn>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				turns.add(new Turn(result.getInt("id_turn"), result
						.getString("description_turn")));
			}
			return turns;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
